package me.zhengjie.pm.projecttask.service.impl;

import me.zhengjie.pm.projecttask.domain.ProjectAllTaskView;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projecttask.repository.ProjectAllTaskViewRepository;
import me.zhengjie.pm.projecttask.service.ProjectAllTaskViewService;
import me.zhengjie.pm.projecttask.service.dto.ProjectAllTaskViewDto;
import me.zhengjie.pm.projecttask.service.dto.ProjectAllTaskViewQueryCriteria;
import me.zhengjie.pm.projecttask.service.mapper.ProjectAllTaskViewMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectAllTaskView")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectAllTaskViewServiceImpl implements ProjectAllTaskViewService {

    private final ProjectAllTaskViewRepository projectAllTaskViewRepository;

    private final ProjectAllTaskViewMapper projectAllTaskViewMapper;

    public ProjectAllTaskViewServiceImpl(ProjectAllTaskViewRepository projectAllTaskViewRepository, ProjectAllTaskViewMapper projectAllTaskViewMapper) {
        this.projectAllTaskViewRepository = projectAllTaskViewRepository;
        this.projectAllTaskViewMapper = projectAllTaskViewMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectAllTaskViewQueryCriteria criteria, Pageable pageable){
        Page<ProjectAllTaskView> page = projectAllTaskViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectAllTaskViewMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectAllTaskViewDto> queryAll(ProjectAllTaskViewQueryCriteria criteria){
        return projectAllTaskViewMapper.toDto(projectAllTaskViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectAllTaskViewDto findById(String id) {
        ProjectAllTaskView projectAllTaskView = projectAllTaskViewRepository.findById(id).orElseGet(ProjectAllTaskView::new);
        ValidationUtil.isNull(projectAllTaskView.getId(),"ProjectAllTaskView","id",id);
        return projectAllTaskViewMapper.toDto(projectAllTaskView);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectAllTaskViewDto create(ProjectAllTaskView resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return projectAllTaskViewMapper.toDto(projectAllTaskViewRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectAllTaskView resources) {
        ProjectAllTaskView projectAllTaskView = projectAllTaskViewRepository.findById(resources.getId()).orElseGet(ProjectAllTaskView::new);
        ValidationUtil.isNull( projectAllTaskView.getId(),"ProjectAllTaskView","id",resources.getId());
        projectAllTaskView.copy(resources);
        projectAllTaskViewRepository.save(projectAllTaskView);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            projectAllTaskViewRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectAllTaskViewDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectAllTaskViewDto projectAllTaskView : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectAllTaskView.getProjectId());
            map.put("项目阶段", projectAllTaskView.getStageId());
            map.put("计划完成时间", projectAllTaskView.getPlanFinishTime());
            map.put("任务类型", projectAllTaskView.getType());
            map.put("项目名称", projectAllTaskView.getProjectName());
            map.put("阶段名称", projectAllTaskView.getStageName());
            map.put("任务简要描述", projectAllTaskView.getSimpleDescribe());
            map.put("安排任务人数", projectAllTaskView.getWorkerNo());
            map.put("当前完成状态", projectAllTaskView.getWorkState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}